<?php
/**
 * @Notes:【】
 * @Date: 2022-06-05 15:23
 */

namespace Form\Ocean;

use App\Dao\Ocean\OceanFishDao;
use App\Dao\Ocean\OceanRubbishDao;
use Form\BaseForm;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/oceanRubbish")
 */
class OceanRubbish extends BaseForm
{
    protected $yjCode = 'oceanrubbish';

    protected $routes = [
        'lists' => '/admin/ocean/rubbish/lists',
        'add' => '/admin/ocean/rubbish/add',
        'edit' => '/admin/ocean/rubbish/edit',
        'form' => '/form/oceanRubbish/update',
    ];


    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('鱼类名称')->setProp('name'),
            Column::txt()->setLabel('出售价格')->setProp('price'),
            Column::txt()->setLabel('成熟时间')->setProp('grow_time'),
            Column::image()->setLabel('图片')->setProp('picture'),
            Column::switch()->setLabel('状态')->setProp('status'),

        ]);
        return $colum->toArray();
    }

    protected function getRoute($types = null)
    {
        if (is_null($types)) {
            return $this->routes;
        }
        return $this->routes[$types];
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        _SUCCESS(compact('routes', 'column'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",default="0")
     */
    public function update()
    {
        $id = $this->getValidatorData('id');

        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));

        $form->setRule([
            Elm::input('name', '鱼类名称')->required(),
            Elm::number('price', '价钱')->required(),
            Elm::YjUpload()->field('picture')->title('图片'),
            Elm::number('grow_time', '成熟时间')->required(),
        ]);

        $id && $form->append(
            Elm::hidden('id', $id)
        );

        $lists = $form->setTitle(($id ? '编辑' : '添加') . '鱼类')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(OceanRubbishDao::class)->where('id', $id)->first();
            }
            return $formData;
        });

        _SUCCESS(formToData($lists));
    }
}